﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManage
{
    public class OrderDetails
    {
        public Goods[] goods { get; set; }
        public int[] numbers { get; set; }
        public int count = 0;
        public OrderDetails()
        {
            goods = new Goods[1];
            numbers = new int[1];
        }

        public void AddGoods(Goods g,int num)
        {
            for(int i = 0; i < count; i++)
            {
                if (g.Equals(goods[i]))
                {
                    numbers[i] += num;
                    return;
                }
            }
            count++;
            if (count>goods.Length)
            {
                Goods[] temp1 = new Goods[goods.Length * 2];
                int[] temp2 = new int[numbers.Length * 2];
                for(int i = 0; i < count - 1; i++)
                {
                    temp1[i] = goods[i];
                    temp2[i] = numbers[i];
                }
                goods = temp1;
                numbers = temp2;
            }
            goods[count - 1] = g;
            numbers[count - 1] = num;
        }
        public void modifyGoods(int index,int num,double price)
        {
            goods[index-1].price = price;
            numbers[index-1] = num;
        }
        public void deleteGoods(int index)
        {
            for(int i = index; i < count-1; i++)
            {
                goods[i] = goods[i + 1];
                numbers[i] = numbers[i + 1];
            }
            goods[count - 1] = null;
            numbers[count - 1] = 0;
            count--;
        }
        public override string ToString()
        {
            string orderDetail="";
            for(int i = 0; i < count; i++)
            {
                orderDetail += goods[i] + ":" + numbers[i] + "个"+"\n";
            }
            return orderDetail;
        }
        public override bool Equals(object obj)
        {
            OrderDetails o = obj as OrderDetails;
            bool[] flags = new bool[count];//初始化一个bool数组，初始值全为false；
            bool flag = true;
            for (int i = 0; i < count; i++)
            {
                for (int j = 0; j < count; j++)
                {

                    if (goods[i].Equals(o.goods[j])&&numbers[i]==o.numbers[j]) { flags[i] = true; }//遇到有相同的值，对应的bool数组的值设为true；

                }
            }
            foreach (var item in flags)
            {
                if (item == false) flag=false; //遍历bool数组，还有false，就说明有不同的值，结果返回false。
            }
            return o!=null&&o.count==count&&flag;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}
